import datetime
import pytz

import coin.exchange.base.kr_rest.native_public_client_base as npubcb

class FTXNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self):
    npubcb.NativePublicClientBase.__init__(self, url="https://ftx.com/api")
    self._valid_kline_period = [15, 60, 300, 900, 3600, 14400, 86400]

  def check_kline_period(self, kline_period):
    assert kline_period in self._valid_kline_period, \
      'Invalid kline period! %s' % kline_period

  def get_history_kline(self, symbol, kline_period, start_time, end_time):
    self.check_kline_period(kline_period)
    params = {'resolution': kline_period, 'limit': 5000,
              'start_time': int(start_time.timestamp()),
              'end_time': int(end_time.timestamp())}
    return self._get_as_json("markets/%s/candles" % symbol, params=params)

  def queryAll(self):
    raise NotImplementedError()


if __name__ == "__main__":
  client = FTXNativePublicClient()
  start_time = datetime.datetime(2020, 11, 5).replace(tzinfo=pytz.UTC)
  end_time = datetime.datetime(2020, 11, 5, 1, 59, 59).replace(tzinfo=pytz.UTC)
  print(client.get_history_kline('BTC/USD', 3600, start_time, end_time).msg)
  print(client.get_history_kline('BTC-1225', 3600, start_time, end_time).msg)
